package com.dmcb.common.business.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by 王勇 on 2018/7/27.
 */
@Service
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存  指定时间  单位：秒
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, int expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key, Object value) {
        Object result;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        result = result == null ? value : result;
        return result;
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void delete(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    public HashMap<Object, Object> getAllRedis() {
        //获取海洛所有的Key
        Set<String> keys = redisTemplate.keys("818*");
        HashMap<Object, Object> map = new HashMap<>();
        //循环
        for (String key : keys) {
            Object value = redisTemplate.opsForValue().get(key);
            map.put(key, value);
        }
        //返回集合
        return map;
    }

    public static void changeIP(int size) {

        String oldIP = JedisHelper.getIp(WebClientUtil.PROXY_REGION);
        int num =0;
        while (true){
            JedisHelper.setIp(WebClientUtil.PROXY_REGION, "");
            DateUtil.sleep(30);
            String newIP =JedisHelper.getIp(WebClientUtil.PROXY_REGION);
            if (!oldIP.equals(newIP) &&   StringUtils.isNotBlank(newIP)){
                break;
            }
            oldIP = JedisHelper.getIp(WebClientUtil.PROXY_REGION);
//            logger.info("尝试重新更换IP，当前代理 = " + WebClientUtil.PROXY_REGION);
            if(++num >5){
                break;
            }
        }

    }

}
